/***************************************************************************
*  ConfigGlobal.cpp
*
*  July 2007
*  Copyright  2007  RuijinZheng,China
*  Email mebetop@126.com
****************************************************************************/

/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
#include <QMessageBox>
#include <string.h>
#include "ConfigGlobal.h"

ConfigGlobal::ConfigGlobal(QWidget *parent,Qt::WindowFlags f,ConfigureBase *conf,bool seperate): QDialog(parent,f)
{
    setupUi(this); 
    connect(pushButtonOK,SIGNAL(clicked()),this,SLOT(saveSettings()));

    if(conf!=NULL){
        if(conf->getSaveLogOrNot()==true)
            this->checkBoxErrorFile->setCheckState(Qt::Checked);
        for(int i=0;i<this->comboBoxMode->count();i++){
        	  if(this->comboBoxMode->itemText(i)==QString(conf->getProxyType().c_str())){
        	     this->comboBoxMode->setCurrentIndex(i);
        	  }
        } 
        this->spinBoxUDPMinPort->setValue(conf->getUdpMinPort());
        this->spinBoxUDPMaxPort->setValue(conf->getUdpMaxPort());
        this->spinBoxTimeOut->setValue(conf->getConnectTimeOut());
        this->settings=conf;
    }
    this->seperate=seperate;



}
ConfigGlobal::~ConfigGlobal()
{
}
void ConfigGlobal::saveSettings()
{
	
	if(!(this->spinBoxUDPMinPort->value()< this->spinBoxUDPMaxPort->value())){
        QMessageBox::warning(this, tr("Attention"),
            tr("UDP port range :min port must smaller than max port!"));
        return;
	}
	this->settings->setConnectTimeOut(this->spinBoxTimeOut->value());
	this->settings->setUdpMaxPort(this->spinBoxUDPMaxPort->value());
	this->settings->setUdpMinPort(this->spinBoxUDPMinPort->value());
	this->settings->setProxyType(this->comboBoxMode->currentText().toStdString());
	if(this->checkBoxErrorFile->checkState()==Qt::Checked){
		this->settings->setSaveLogOrNot(true);
	}
	else{
		this->settings->setSaveLogOrNot(false);
	}
	this->settings->writeSettings();
	emit this->settingsHaveBeenChanged();
    QMessageBox::warning(this, tr("Information"),
        tr("Settings saved!"));
    if(this->seperate==false){
    	  emit this->rollUp();
    }else{
    	  this->accept();
    }
}
